package com.learn.ssm.handler;

import com.learn.ssm.pojo.Schedule;
import com.learn.ssm.service.api.ScheduleService;
import com.learn.ssm.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/schedule")
@Slf4j
public class ScheduleController {
    @Autowired
    private ScheduleService scheduleService;

    @GetMapping("/{pageSize}/{currentPage}")
    public R page(
            @PathVariable Integer pageSize,
            @PathVariable Integer currentPage
    ) {
        R r = scheduleService.page(pageSize, currentPage);
        log.info("查询的数据为: {}", r.toString());
        return r;
    }

    @DeleteMapping("/{id}")
    public R remove(@PathVariable Integer id) {
        return scheduleService.remove(id);
    }

    @PostMapping
    public R save(@RequestBody @Validated Schedule schedule,
                  BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            return R.fail("参数传递错误");
        }
        return scheduleService.save(schedule);
    }

    @PutMapping
    public R update(@RequestBody Schedule schedule) {
        return scheduleService.update(schedule);
    }
}
